﻿using System;
using UnityEngine;

namespace UltimateFracturing
{
	// Token: 0x02000445 RID: 1093
	[Serializable]
	public class SupportPlane
	{
		// Token: 0x06001E88 RID: 7816 RVA: 0x000C3ED8 File Offset: 0x000C20D8
		public SupportPlane(FracturedObject fracturedObject)
		{
			this.GUIExpanded = true;
			this.GUIName = "New Support Plane";
			this.GUIShowInScene = true;
			this.fracturedObject = fracturedObject;
			this.planeMesh = new Mesh();
			Vector3[] array = new Vector3[4];
			Vector3[] array2 = new Vector3[4];
			Vector2[] uv = new Vector2[4];
			int[] triangles = new int[]
			{
				0,
				1,
				2,
				0,
				2,
				3
			};
			array[0] = new Vector3(-1f, 0f, -1f);
			array[1] = new Vector3(-1f, 0f, 1f);
			array[2] = new Vector3(1f, 0f, 1f);
			array[3] = new Vector3(1f, 0f, -1f);
			array2[0] = new Vector3(0f, 1f, 0f);
			array2[1] = new Vector3(0f, 1f, 0f);
			array2[2] = new Vector3(0f, 1f, 0f);
			array2[3] = new Vector3(0f, 1f, 0f);
			bool flag = false;
			float num = 1f;
			if (fracturedObject.SourceObject && fracturedObject.SourceObject.GetComponent<Renderer>())
			{
				flag = true;
			}
			if (flag)
			{
				Bounds bounds = fracturedObject.SourceObject.GetComponent<Renderer>().bounds;
				num = bounds.extents.y;
				for (int i = 0; i < array.Length; i++)
				{
					float num2 = 1.3f;
					float num3 = Mathf.Max(bounds.extents.z * num2, bounds.extents.x * num2);
					array[i] = Vector3.Scale(array[i], new Vector3(num3, num3, num3)) + fracturedObject.transform.position;
					array[i] = fracturedObject.transform.InverseTransformPoint(array[i]);
				}
				this.v3PlanePosition = fracturedObject.transform.position - new Vector3(0f, num - 0.05f, 0f);
				this.v3PlanePosition = fracturedObject.transform.InverseTransformPoint(this.v3PlanePosition);
				this.qPlaneRotation = Quaternion.identity;
			}
			else
			{
				for (int j = 0; j < array.Length; j++)
				{
					array[j] += fracturedObject.transform.position;
					array[j] = fracturedObject.transform.InverseTransformPoint(array[j]);
				}
				this.v3PlanePosition = new Vector3(0f, -num * 0.5f + 0.05f, 0f);
				this.qPlaneRotation = Quaternion.identity;
			}
			this.v3PlaneScale = Vector3.one;
			this.planeMesh.vertices = array;
			this.planeMesh.normals = array2;
			this.planeMesh.uv = uv;
			this.planeMesh.triangles = triangles;
		}

		// Token: 0x06001E89 RID: 7817 RVA: 0x000C4263 File Offset: 0x000C2463
		public Matrix4x4 GetLocalMatrix()
		{
			return Matrix4x4.TRS(this.v3PlanePosition, this.qPlaneRotation, this.v3PlaneScale);
		}

		// Token: 0x06001E8A RID: 7818 RVA: 0x000C427C File Offset: 0x000C247C
		public Vector3[] GetBoundingBoxSegments(Bounds bounds)
		{
			Vector3 min = bounds.min;
			Vector3 max = bounds.max;
			Vector3[] array = new Vector3[]
			{
				new Vector3(min.x, min.y, min.z),
				new Vector3(min.x, min.y, max.z),
				new Vector3(max.x, min.y, max.z),
				new Vector3(max.x, min.y, min.z),
				new Vector3(min.x, max.y, min.z),
				new Vector3(min.x, max.y, max.z),
				new Vector3(max.x, max.y, max.z),
				new Vector3(max.x, max.y, min.z)
			};
			Vector3[] array2 = new Vector3[24];
			for (int i = 0; i < 4; i++)
			{
				array2[i * 2] = array[i % 4];
				array2[i * 2 + 1] = array[(i + 1) % 4];
			}
			for (int j = 4; j < 8; j++)
			{
				array2[j * 2] = array[j % 4 + 4];
				array2[j * 2 + 1] = array[(j + 1) % 4 + 4];
			}
			for (int k = 8; k < 12; k++)
			{
				array2[k * 2] = array[k % 4];
				array2[k * 2 + 1] = array[k % 4 + 4];
			}
			return array2;
		}

		// Token: 0x06001E8B RID: 7819 RVA: 0x000C44E0 File Offset: 0x000C26E0
		public bool IntersectsWith(GameObject otherGameObject, bool bBelowIsAlsoValid = false)
		{
			MeshFilter component = otherGameObject.GetComponent<MeshFilter>();
			if (this.planeMesh == null || component == null)
			{
				return false;
			}
			Vector3[] vertices = this.planeMesh.vertices;
			Matrix4x4 matrix4x = this.fracturedObject.transform.localToWorldMatrix * this.GetLocalMatrix();
			for (int i = 0; i < 4; i++)
			{
				vertices[i] = matrix4x.MultiplyPoint3x4(vertices[i]);
				vertices[i] = otherGameObject.transform.InverseTransformPoint(vertices[i]);
			}
			Plane plane = new Plane(vertices[0], vertices[1], vertices[2]);
			Vector3 normalized = (vertices[1] - vertices[0]).normalized;
			Vector3 normalized2 = (vertices[2] - vertices[1]).normalized;
			Matrix4x4 inverse = Matrix4x4.TRS(vertices[0], Quaternion.LookRotation(normalized, Vector3.Cross(normalized, normalized2)), Vector3.one).inverse;
			float magnitude = (vertices[2] - vertices[1]).magnitude;
			float fLimitUp = component.sharedMesh.bounds.max.y - component.sharedMesh.bounds.min.y;
			float magnitude2 = (vertices[1] - vertices[0]).magnitude;
			Vector3[] boundingBoxSegments = this.GetBoundingBoxSegments(component.sharedMesh.bounds);
			for (int j = 0; j < 12; j++)
			{
				if (SupportPlane.TestSegmentVsPlane(boundingBoxSegments[j * 2], boundingBoxSegments[j * 2 + 1], plane, inverse, magnitude, fLimitUp, magnitude2))
				{
					return true;
				}
			}
			if (bBelowIsAlsoValid)
			{
				float distanceToPoint = plane.GetDistanceToPoint(component.sharedMesh.bounds.center);
				if (distanceToPoint < 0f)
				{
					for (int k = 0; k < 24; k++)
					{
						boundingBoxSegments[k] += plane.normal * -distanceToPoint;
					}
					for (int l = 0; l < 12; l++)
					{
						if (SupportPlane.TestSegmentVsPlane(boundingBoxSegments[l * 2], boundingBoxSegments[l * 2 + 1], plane, inverse, magnitude, fLimitUp, magnitude2))
						{
							return true;
						}
					}
				}
			}
			return false;
		}

		// Token: 0x06001E8C RID: 7820 RVA: 0x000C47F0 File Offset: 0x000C29F0
		private static bool TestSegmentVsPlane(Vector3 v1, Vector3 v2, Plane plane, Matrix4x4 mtxToPlaneLocal, float fLimitRight, float fLimitUp, float fLimitForward)
		{
			float num = v1.x * plane.normal.x + v1.y * plane.normal.y + v1.z * plane.normal.z + plane.distance;
			float num2 = v2.x * plane.normal.x + v2.y * plane.normal.y + v2.z * plane.normal.z + plane.distance;
			if (num * num2 > 0f)
			{
				return false;
			}
			float num3 = 0f;
			Ray ray = new Ray(v1, (v2 - v1).normalized);
			if (plane.Raycast(ray, out num3))
			{
				Vector3 point = v1 + (v2 - v1).normalized * num3;
				Vector3 vector = mtxToPlaneLocal.MultiplyPoint3x4(point);
				if (num3 <= fLimitUp && vector.x >= 0f && vector.x <= fLimitRight && vector.z >= 0f && vector.z <= fLimitForward)
				{
					return true;
				}
			}
			return false;
		}

		// Token: 0x04001F02 RID: 7938
		public bool GUIExpanded;

		// Token: 0x04001F03 RID: 7939
		public string GUIName;

		// Token: 0x04001F04 RID: 7940
		public bool GUIShowInScene;

		// Token: 0x04001F05 RID: 7941
		public Vector3 v3PlanePosition;

		// Token: 0x04001F06 RID: 7942
		public Quaternion qPlaneRotation;

		// Token: 0x04001F07 RID: 7943
		public Vector3 v3PlaneScale;

		// Token: 0x04001F08 RID: 7944
		public Mesh planeMesh;

		// Token: 0x04001F09 RID: 7945
		public FracturedObject fracturedObject;
	}
}
